﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Data.OleDb;

namespace CarBlackBox
{
    public partial class Form_ProductManage : Form
    {
        public Form_ProductManage()
        {
            InitializeComponent();
        }

        private void Form_ProductManage_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“systemDBDataSet.RecordInfo”中。您可以根据需要移动或移除它。
            this.recordInfoTableAdapter.Fill(this.systemDBDataSet.RecordInfo);

        }
        private delegate void renovateRecordInfo();
        public void RenovateRecordInfo()
        {
            this.Invoke(new renovateRecordInfo(delegate() { this.recordInfoTableAdapter.Fill(this.systemDBDataSet.RecordInfo); }));
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count == 0)
                return;
            if (WFNetLib.WFGlobal.MessageYseOrNo("确认删除当前的选定项么？", "删除") == DialogResult.No)
                return;
            string rin = RecordInfoDataFactory.GetPrimaryKey(dgv, dgv.SelectedRows[0].Index);
            if (recordInfoTableAdapter.DeleteQuery(rin) == 1)
            {
                MessageBox.Show("删除成功");
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.记录仪管理.ToString();
                log.LogContent = "删除:" + rin;
                log.LogRemark = "记录仪删除";
                log.InserSQLLog();
                this.recordInfoTableAdapter.Fill(this.systemDBDataSet.RecordInfo); 
            }
            else
            {
                MessageBox.Show("删除中出现某些问题，请联系管理员");
            }            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form_ProductEditor f = new Form_ProductEditor(this);
            f.ShowDialog();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            WFNetLib.ExcelExport ExcelEx = new WFNetLib.ExcelExport(dgv.Rows.Count);
            ExcelEx.ExcelWorkbookCallbackProc = new WFNetLib.ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
            if (dgv.Rows.Count > 65530)
            {
                MessageBox.Show("条目过多，无法导出，请重新选择索引条件");
                return;
            }
            if (dgv.Rows.Count == 0)
            {
                MessageBox.Show("当前没有显示任何内容，无法导出");
                return;
            }
            ExcelEx.ExcelExportProc();
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.记录仪管理.ToString();
            log.LogContent = "导出记录仪序列号" + dgv.Rows.Count.ToString() + "条";
            log.LogRemark = "记录仪导出";
            log.InserSQLLog();
        }
        private void ExcelWorkbookCallbackProc(Workbook wBook, int index)
        {
            Worksheet wSheet = (Worksheet)wBook.Worksheets[wBook.Worksheets.Count];
            Range dr;
            if (index == -1)
            {
                wSheet.Name = "记录仪序列号";
                wSheet.Cells[1, 1] = "编号";
                wSheet.Cells[1, 2] = "记录仪序列号";
                dr = wSheet.get_Range("A1", "B1");
                dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                dr.Interior.Pattern = XlPattern.xlPatternSolid;
            }
            else if (index == int.MaxValue)
            {
                dr = wSheet.get_Range("A1", "B" + (dgv.Rows.Count + 1).ToString());
                dr.Columns.AutoFit();
                dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                dr.Borders.LineStyle = XlLineStyle.xlContinuous;
            }
            else
            {
                RecordInfoData r = RecordInfoDataFactory.Construct(dgv, index);
                wSheet.Cells[2 + index, 1] = r.ID.ToString();
                wSheet.Cells[2 + index, 2] = r.RIN;
                if (index % 2 == 1)
                {
                    dr = wSheet.get_Range("A" + (2 + index).ToString(), "B" + (2 + index).ToString());
                    dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
                    dr.Interior.Pattern = XlPattern.xlPatternSolid;
                }
            } 
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(toolStripComboBox2.Text))
            {
                this.recordInfoTableAdapter.Fill(this.systemDBDataSet.RecordInfo);
                return;
            }
            if (模糊查询ToolStripMenuItem.Checked)
            {
                this.recordInfoTableAdapter.FillByBlurRIN(this.systemDBDataSet.RecordInfo, toolStripComboBox2.Text);
            }
            else
            {
                this.recordInfoTableAdapter.FillByRIN(this.systemDBDataSet.RecordInfo, toolStripComboBox2.Text);
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            WFNetLib.ExcelImport ExcelIn = new WFNetLib.ExcelImport(true, "记录仪序列号");
            System.Data.DataTable dt;
            dt = ExcelIn.ExcelImportProc_OleDB();
            int count = dt.Rows.Count;
            ArrayList failList = new ArrayList();
            if (dt != null)
            {
                if(dt.Rows.Count==0)
                {
                    MessageBox.Show("当前没有任何数据需要导入");
                    return;
                }
                foreach (DataRow dr in dt.Rows)
                {
                    int n=0;
                    try
                    {
                        n = recordInfoTableAdapter.Insert(dr["记录仪序列号"].ToString());
                    }
                    catch (OleDbException ex)
                    {
                        if (ex.ErrorCode == -2147467259)//主键已经存在
                        {
                            failList.Add(dr["记录仪序列号"]);
                            continue;
                        }
                    }
                    if (n != 1)
                    {
                        MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
                        return;
                    }
                }
            }
            if (failList.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Object str in failList)
                {
                    sb.Append(str.ToString()+"\r\n");
                }
                sb.Append("共有"+failList.Count+"条数据已经存在在数据库中");
                MessageBox.Show(sb.ToString());
            }
            MessageBox.Show("共导入"+(dt.Rows.Count-failList.Count).ToString()+"条数据");
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.记录仪管理.ToString();
            log.LogContent = "导入记录仪序列号" + (dt.Rows.Count - failList.Count).ToString() + "条";
            log.LogRemark = "记录仪导入";
            log.InserSQLLog();
        }
    }
}
